package com.hgkj.cloud.customer.controller;

import com.hgkj.cloud.base.service.HcChannelsService;
import com.hgkj.cloud.customer.common.ResponseData;
import com.hgkj.cloud.base.domain.TenantFrameworks;
import com.hgkj.cloud.base.service.EUserService;
import com.hgkj.cloud.base.service.TenantFrameworksService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping("/TenantFrameworks")
public class TenantFrameworksController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static String FREE_DAY = "free_day";

    @Autowired
    private HcChannelsService hcChannelsService;
    @Autowired
    private TenantFrameworksService mTenantFrameworksService;
    @Autowired
    private EUserService eUserService;

    @RequestMapping("/addTenantFrameworks")
    @ResponseBody
    public ResponseData addTenantFrameworks(String frameworkName, Long fatherId) {
        ResponseData resData = new ResponseData();
        Map<String, Object> map = new HashMap<>();
        if (frameworkName == null || "".equals(frameworkName)) {
            resData.setCode(-1);
            resData.setMsg("添加失败,frameworkName为空");
            return resData;
        }
        if (fatherId == null) {
            resData.setCode(-1);
            resData.setMsg("添加失败,fatherId为空");
            return resData;
        }
        try {
            TenantFrameworks frameworkById = mTenantFrameworksService.getFrameworkById(fatherId);
            if (frameworkById == null) {
                resData.setCode(-1);
                resData.setMsg("添加失败,fatherId错误");
                return resData;
            }
            TenantFrameworks mTenantFrameworks = new TenantFrameworks();
            mTenantFrameworks.setName(frameworkName);
            mTenantFrameworks.setWhole_name(frameworkById.getWhole_name() + "/" + frameworkName);
            mTenantFrameworks.setChannel_id(frameworkById.getChannel_id());
            mTenantFrameworks.setFather_id(fatherId);
            mTenantFrameworksService.insertSelective(mTenantFrameworks);
            map.put("mTenantFrameworks", mTenantFrameworks);
            resData.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            resData.setCode(-1);
            resData.setMsg("添加失败");
        }
        return resData;
    }

    @RequestMapping("/updateTenantFrameworks")
    @ResponseBody
    public ResponseData updateTenantFrameworks(Long frameworkId, String frameworkName, Integer enabled) {
        ResponseData resData = new ResponseData();
        logger.info("===========updateTenantFrameworks=======");
        logger.info("=====frameworkId: "+frameworkId +"   frameworkName:  "+frameworkName+"  enabled:"+enabled);
        Map<String, Object> map = new HashMap<>();
        if (frameworkId == null) {
            resData.setCode(-1);
            resData.setMsg("添加失败,fatherId为空");
            return resData;
        }
        try {
            TenantFrameworks frameworkById = mTenantFrameworksService.getFrameworkById(frameworkId);
            if (frameworkById == null) {
                resData.setCode(-1);
                resData.setMsg("添加失败,frameworkId错误");
                return resData;
            }
            if (frameworkName != null && !"".equals(frameworkName)) {
                map.put("name", frameworkName);
                map.put("father_id", frameworkById.getFather_id());
                List<TenantFrameworks> tenantFrameworks = mTenantFrameworksService.listAllFrameworkByMap(map);
                if (tenantFrameworks != null && tenantFrameworks.size() != 0) {
                    resData.setCode(-1);
                    resData.setMsg("添加失败,frameworkName重复");
                    return resData;
                } else {
                    frameworkById.setName(frameworkName);
                }
            }
            if (enabled != null) {
                frameworkById.setEnabled(enabled);
            }
            mTenantFrameworksService.updateByPrimaryKeySelective(frameworkById);
            if (enabled != null && "0".equals(enabled.toString())) {
                logger.info("=======删除用户信息=====");
                eUserService.updateUserByFrameworkId(frameworkId);
            }

            map.put("mTenantFrameworks", frameworkById);
            resData.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            resData.setCode(-1);
            resData.setMsg("添加失败");
        }
        return resData;
    }
}